package com.loto.insurance.oem.web.controller.ajax;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.loto.insurance.oem.web.authority.OEMAuthority;
import com.loto.insurance.oem.web.entity.OrderInfo;
import com.loto.insurance.oem.web.entity.SessionUser;
import com.loto.insurance.oem.web.entity.ajax.BaseJson;
import com.loto.insurance.oem.web.enumeration.ControllerPropertyEnum;
import com.loto.insurance.oem.web.enumeration.ResultTypeEnum;
import com.loto.insurance.oem.web.service.OrderInfoService;
import com.loto.insurance.oem.web.service.PolicyInfoService;
import com.loto.insurance.oem.web.service.common.AirIns;
import com.loto.insurance.oem.web.service.common.PolicyOrder;
import com.loto.insurance.oem.web.service.common.TravelIns;

@Controller
@SessionAttributes("oem_user")
public class OrderInfoController {
  @Autowired
  OrderInfoService orderInfoService;

  @Autowired
  PolicyInfoService policyInfoService;

  @Autowired
  PolicyOrder policyOrder;

  @OEMAuthority(resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/order/create.json", method = RequestMethod.POST)
  public ModelAndView orderCreate(
	  @RequestParam(value = "arrList[]", required = true) ArrayList<String> arrList,
	  @RequestParam(value = "productId", required = true) long productId,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {
	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = orderInfoService.createOrder(arrList, productId,
	    sessionUser.getUserId(), sessionUser.getUsername());
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/order/upload.json", method = RequestMethod.POST)
  public ModelAndView orderUpload(
	  @RequestParam(value = "file", required = true) MultipartFile file,
	  @RequestParam(value = "pt", required = true) String insType) {
	ModelAndView mav = new ModelAndView();
	String fileName = file.getOriginalFilename();
	String fileType = fileName.substring(fileName.lastIndexOf(".") + 1,
	    fileName.lastIndexOf(".") + 4);

	BaseJson baseJson = new BaseJson();

	if (!fileType.toLowerCase().equals("xls")) {
	  baseJson.setMsg(ControllerPropertyEnum.NOT_EXCEL.toString());
	  baseJson.setOk(false);
	} else {
	  switch (insType) {
		case "air":
		  baseJson = new AirIns().parseExcel(file);
		  break;
		case "travel":
		  baseJson = new TravelIns().parseExcel(file);
		  break;
		default:
		  baseJson.setMsg(ControllerPropertyEnum.ORDER_INPUT_WRONG.toString());
		  baseJson.setOk(false);
	  }
	}
	mav.addObject("result", baseJson);
	return mav;
  }

  @OEMAuthority(resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/order/cancel.json", method = RequestMethod.GET)
  public ModelAndView orderCancel(
	  @RequestParam(value = "orderId", required = true) long orderId,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {
	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = orderInfoService.cancelOrder(orderId, sessionUser.getUserId());
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/policy/cancel.json", method = RequestMethod.GET)
  public ModelAndView policyCancel(
	  @RequestParam(value = "policyNo", required = true) String policyNo,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {
	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = policyOrder.cancelPolicy(policyNo, sessionUser.getUserId());
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/policy/refund.json", method = RequestMethod.GET)
  public ModelAndView policyRefund(
	  @RequestParam(value = "orderId", required = true) long orderId,
	  @RequestParam(value = "policyId", required = true) long policyId,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {
	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base = policyOrder.refundPolicy(orderId, policyId, sessionUser.getUserId());
	mav.addObject("result", base);
	return mav;
  }

  @OEMAuthority(resultType = ResultTypeEnum.json)
  @RequestMapping(value = "/policy/view_problem.json", method = RequestMethod.GET)
  public ModelAndView policyViewProblem(
	  @RequestParam(value = "oid", required = true) long orderId,
	  @RequestParam(value = "pid", required = true) long policyId,
	  @ModelAttribute("oem_user") SessionUser sessionUser) {
	ModelAndView mav = new ModelAndView();
	BaseJson base = new BaseJson();
	base.setOk(false);
	OrderInfo orderInfo = orderInfoService.findOrderInfoById(orderId,
	    sessionUser.getUserId());
	if (orderInfo != null) {
	  base.setMsg(policyInfoService.findLastestFlowByIdType(
		  orderInfo.getOrderProductTypeFid(), policyId));
	  base.setOk(true);
	}
	mav.addObject("result", base);
	return mav;
  }
}
